package com.free.web.controller.system.controller;

import com.free.common.annotation.Log;
import com.free.common.core.controller.BaseController;
import com.free.common.core.domain.AjaxResult;
import com.free.common.core.page.TableDataInfo;
import com.free.common.enums.BusinessType;
import com.free.common.utils.poi.ExcelUtil;
import com.free.system.domain.FreeMember;
import com.free.system.service.IFreeMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员信息Controller
 *
 * @author Sophia
 * @date 2025-02-25
 */
@Controller
@RequestMapping("/system/member")
public class FreeMemberController extends BaseController {
    private String prefix = "system/member";

    @Autowired
    private IFreeMemberService freeMemberService;

    @RequiresPermissions("system:member:view")
    @GetMapping()
    public String member() {
        return prefix + "/member";
    }

    /**
     * 查询会员信息列表
     */
    @RequiresPermissions("system:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FreeMember freeMember) {
        startPage();
        List<FreeMember> list = freeMemberService.selectFreeMemberList(freeMember);
        return getDataTable(list);
    }

    /**
     * 获取全部会员信息
     *
     * @return
     */
    @GetMapping("/getMemberList")
    @ResponseBody
    public AjaxResult getList() {
        List<FreeMember> list = freeMemberService.selectFreeMemberList(null);
        return AjaxResult.success(list);
    }

    /**
     * 导出会员信息列表
     */
    @RequiresPermissions("system:member:export")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FreeMember freeMember) {
        List<FreeMember> list = freeMemberService.selectFreeMemberList(freeMember);
        ExcelUtil<FreeMember> util = new ExcelUtil<FreeMember>(FreeMember.class);
        return util.exportExcel(list, "会员信息数据");
    }

    /**
     * 新增会员信息
     */
    @RequiresPermissions("system:member:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存会员信息
     */
    @RequiresPermissions("system:member:add")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FreeMember freeMember) {
        return toAjax(freeMemberService.insertFreeMember(freeMember));
    }

    /**
     * 修改会员信息
     */
    @RequiresPermissions("system:member:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FreeMember freeMember = freeMemberService.selectFreeMemberById(id);
        mmap.put("freeMember", freeMember);
        return prefix + "/edit";
    }

    /**
     * 修改保存会员信息
     */
    @RequiresPermissions("system:member:edit")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FreeMember freeMember) {
        return toAjax(freeMemberService.updateFreeMember(freeMember));
    }

    /**
     * 删除会员信息
     */
    @RequiresPermissions("system:member:remove")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(freeMemberService.deleteFreeMemberByIds(ids));
    }
}
